package com.example.android_web_4;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.entity.TbUsers;
import com.example.utils.CallBack;
import com.example.utils.HttpUtils;
import com.example.utils.MyShareApplication;
import com.google.gson.Gson;

public class MainActivity extends Activity {

    private EditText uname,upwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        uname=(EditText)findViewById(R.id.uname);
        upwd=(EditText)findViewById(R.id.upwd);
    }

    //登录
    public void doLogin(View view){
        final String name=uname.getText().toString();
        String pwd=upwd.getText().toString();
        String basePath="http://"+getResources().getString(R.string.ipaddress)+":"+getResources().getString(R.string.serverport)+"/" +
                getResources().getString(R.string.project);
        String url= basePath+"/TbUserServlet";
        String data="method=login&uname="+name+"&upwd="+pwd;
        HttpUtils.doPost(url, data, new CallBack() {
            @Override
            public void success(final String str) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(str.length()>0){
                            SharedPreferences share=getSharedPreferences("lock",MODE_PRIVATE);
                            SharedPreferences.Editor editor=share.edit();
                            TbUsers users=new Gson().fromJson(str,TbUsers.class);
                            editor.putInt("uid",users.get_id());
                            editor.putString("uname",users.getUname());
                            editor.putString("urealname", users.getUrealname());
                            editor.commit();
                            //Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                            //跳转操作
                            Intent intent=new Intent(MainActivity.this,StuActivity.class);
                            startActivity(intent);
                        }else{
                            Toast.makeText(MainActivity.this, "用户名和密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
            @Override
            public void failed(String str) {
                Log.i("Login Error",name+"登录出现错误");
            }
        });
    }

    //去注册用户
    public void toRegister(View view){
        Intent intent=new Intent(this,UserRegisterActivity.class);
        startActivityForResult(intent,1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            if(requestCode==1){
                String name=data.getStringExtra("userName");
                uname.setText(name);
            }
        }
    }
}
